home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
UTILITY
/
DO1002.ARJ
/
READ.SCR
< prev
next >
Wrap
Text File
|
1991-12-28
|
5KB
|
155 lines
.pg wi full clr cy
COMMAND NAME»gray«: »%t«ReadFromFile»ye«
/READ {handle} {delimiter} {variable} {variable} . . .
/cw
The »%t«ReadFromFile»#« command reads lines from »ye«ASCII TEXT»#« files.
The file must have been opened with the »%t«/OPEN»#« command in read mode.
The lines are expected to be in the format:
»gr«
{field}»cy«{delimiter}»gr«{field}»cy«{delimiter}»gy« . . .
»#«
Up to »wh«17 fields»#« (»cy«variables»#«) can be read from a file line.
The fields are placed into the named variables in the order
they are parsed from the record. »ye«If there are more fields
than there are variables, the remaining fields will be
ignored.»#«
The command jumps to the following labels on the indicated
conditions:
»wh«{READERROR}»#« Any error opening or reading the file.
»wh«{EOF}»#« When end of file is reached. This is the
normal process if you are reading an entire
file using a loop.
All variables are »ye«TEXT»#« variables. The entire line read is
stored in the »ye«TEXT»#« variable »cy«FILEBUFFER»#«. Each time a line is
read, »cy«FILEBUFFER»#« is overlaid. Therefore if you wish to keep
a particular record, you must move it to another variable.
.pg clr
/GOSUB DispCode
»go 1 24 ye«You might have this code.»#«
/SET Blank " "
.pg -24
/GOSUB DispCode
.go 1 5
%blank(L60)
The repeat loop causes the program to read the file until
»cy«VAR1»#« equals the string stored in »cy«ANSWER»#«. This could be used
to look up some parameters for a particular person, for
example.%blank(L40)
%blank(L60)
.pg -24
/GOSUB DispCode
.go 1 9
%blank(L60)
If »cy«VAR1»#« never equals »cy«ANSWER»#« then when the end of file is
reached, the system will jump to the label »ye«:EOF»#« and set »cy«VAR2»#«
and »cy«VAR3»#« to "»gr«NOT FOUND»#«".%blank(L40)
%blank(L60)
.pg -24
/GOSUB DispCode
.go 1 6
%blank(L60)
If there is some error reading the file, then the script will
jump to the label "»re«:READERROR»#«", display a message and exit.
%blank(L60)
.pg -24 clr
Let's set up a file to read from called »ye«READ.TMP»#«. We'll open
it an write some records to it using the »%t«/OPEN»#«, »%t«/WRITE»#«
and »%t«/CLOSE»#« commands.
/OPEN 1 READ.TMP W
/WRITE 1 "One,Field 1.1,Field 1.2,Field 1.3"
/WRITE 1 "Two,Field 2.1,Field 2.2,Field 2.3"
/WRITE 1 "Three,Field 3.1,Field 3.2,Field 3.3"
/WRITE 1 "Four,Field 4.1,Field 4.2,Field 4.3"
/WRITE 1 "Five,Field 5.1,Field 5.2,Field 5.3"
/CLOSE 1
Press »bo«<enter>»#« to list the file, press »bo«<esc>»#« to exit »ye«SHOW.COM»#«
when you are done . . .
/PAGE
/LIST READ.TMP
.clr
Now we'll find one of the records and read in it's field. We'll use
a loop like this to do the job:
»wh«
/OPEN 1 READ.TMP R
/REPEAT
»%t«/READ»wh« 1 , Key Field1 Field2 Field3
Record: %%FileRecord
/PAGE -24 »cy«<- To give you time to see the record»wh«
/UNTIL %Key EQ %Target »cy«press »bo«<enter>»cy« when you have finished
examining the record.
»#«
.pg
/OPEN 1 READ.TMP R
First, we'll search for Record "»gr«Two»#«".
|/SET Target TWO
/GOSUB FindRecord
As you can see, the case of the Key field does not matter to the
match.
.pg clr
Now, let's look for a key that isn't there, "Six".
|/SET Target SIX
/GOSUB FindRecord
When the loop hit the end-of-file, »%t«DO»#« branched to "»ye«:EOF»#«" and the
line above was printed. Notice that »%t«DO»#« does »re«NOT»#« close the file
unless you explicity use the "»%t«/CLOSE»#«" command.
If we had issued the »%t«/CLOSE»#« command between searches, »%t«DO»#« would have
closed the file after the first search.
We could then have opened it with the »%t«/OPEN»#« command and the
search would have started from the »ye«first record»#« in the file.
/GOTO END
:FindRecord
/REPEAT
/READ 1 , Key Field1 Field2 Field3
»ma«Record»#«: »wh«%FileBuffer»#«
/PAGE -24
/UNTIL %Key EQ %Target
»gr«Record Found»cy«:»wh« %Key »gr«-»ye« %Field1»gr« - »ye«%Field2 »gr«- »ye«%Field3»#«
/RETURN
:EOF
:READERROR
»wh«Record »+re«NOT»wh« Found for »gr«%Target»#«
/RETURN
:DispCode
.clr wh
/OPEN 1 MYFILE.TXT R
/REPEAT
»%t«/READ»wh« 1 "," Var1 Var2 Var3
/UNTIL Var1 EQ %%Answer
/GOTO FOUND
:EOF
/SET Var2 "NOT FOUND"
/SET Var3 "NOT FOUND"
/GOTO FOUND
:READERROR
Unable to Open or Read file! Aborting!
/GOTO END
:FOUND
Your parameters are %%Var2, %%Var3
:END
/CLOSE 1»#«
/RETURN
:END
/CLOSE 1
/ENDEXEC CLEAR